package com.tg.service.protoType;

import com.tg.service.Application;
import com.tg.service.utils.ContextUtil;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
@EnableDiscoveryClient
public class ProtoTypeTest {

    public static void main(String[] args) {
        ConfigurableApplicationContext ctx = SpringApplication.run(ProtoTypeTest.class, args);
        ContextUtil.setApplicationContext(ctx);
    }

    /**
     * 测试scope prototype情况
     *
     * @return
     */
    @RequestMapping(value = "/indexScope")
    public String indexScope() {
        PrototypeEntity var1 = ContextUtil.getBean("PrototypeEntity-singleton");
        System.out.println("singleton" + var1);
        var1.setAmount(var1.getAmount() + 1);
        System.out.println("singleton" + var1);

        PrototypeEntity var2 = ContextUtil.getBean("PrototypeEntity-singleton");
        System.out.println("singleton" + var2);


        PrototypeEntity var3 = ContextUtil.getBean("PrototypeEntity-prototype");
        System.out.println("prototype" + var3);
        var3.setAmount(var3.getAmount() + 1);
        System.out.println("prototype" + var3);

        PrototypeEntity var4 = ContextUtil.getBean("PrototypeEntity-prototype");
        System.out.println("prototype" + var4);

        /**
         * singleton{"amount":1}
         singleton{"amount":2}
         singleton{"amount":2}
         初始化
         prototype{"amount":1}
         prototype{"amount":2}
         初始化
         prototype{"amount":1}
         */
        return "ok";
    }
}
